package com.mo.lib.utils

import android.graphics.*
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Parcel
import android.text.Layout
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.*
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.Nullable
import com.dylanc.longan.application
import com.dylanc.longan.sp
import com.mo.lib.utils.res.ResUtil

/**
 * @ author：mo
 * @ data：2021/10/14:15:16
 * @ 功能：Span样式：https://blog.csdn.net/liao277218962/article/details/50623722
 */
class SpannableStringUtil {
    val spannableStringBuilderList = mutableListOf<SpannableStringBuilder>()

    /** 添加内容*/
    fun add(string: String, block: (SpannableStringBuilder.() -> Unit)? = null): SpannableStringUtil {
        val spannableStringBuilder = SpannableStringBuilder()
        spannableStringBuilder.append(string)
        block?.let {
            spannableStringBuilder.block()
        }
        spannableStringBuilderList.add(spannableStringBuilder)
        return this
    }

    fun getBuilder(): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder()
        for (spannableStringBuilderItem in spannableStringBuilderList) {
            spannableStringBuilder.append(spannableStringBuilderItem)
        }
        return spannableStringBuilder
    }

    /** 设置填充控件，默认设置控件高亮色为透明色以规避设置点击span后自带的淡绿背景，太特么难看了*/
    fun inToView(textView: TextView) { //        val spannableStringBuilder = SpannableStringBuilder()
        //        for (spannableStringBuilderItem in spannableStringBuilderList) {
        //            spannableStringBuilder.append(spannableStringBuilderItem)
        //        }
        textView.setText(getBuilder())
        textView.highlightColor = Color.TRANSPARENT
        textView.movementMethod = LinkMovementMethod.getInstance()
    }
}

/** 设置图片-Bitmap   mVerticalAlignment: ALIGN_BASELINE==1,ALIGN_BOTTOM==0,ALIGN_CENTER==2*/
fun SpannableStringBuilder.setImage(bitmap: Bitmap, mVerticalAlignment: Int = 0, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setImage(ImageSpan(application, bitmap, mVerticalAlignment), defFlags)
}

/** 设置图片-Drawable*/
fun SpannableStringBuilder.setImage(drawable: Drawable, mVerticalAlignment: Int = 0, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setImage(ImageSpan(drawable, mVerticalAlignment), defFlags)
}

/** 设置图片-Uri*/
fun SpannableStringBuilder.setImage(uri: Uri, mVerticalAlignment: Int = 0, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setImage(ImageSpan(application, uri, mVerticalAlignment), defFlags)
}

/** 设置图片-DrawableRes*/
fun SpannableStringBuilder.setImage(@DrawableRes resourceId: Int, mVerticalAlignment: Int = 0, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setImage(ImageSpan(application, resourceId, mVerticalAlignment), defFlags)
}

/** 设置图片-ImageSpan*/
fun SpannableStringBuilder.setImage(imageSpan: ImageSpan, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(imageSpan, 0, length, defFlags)
}

/** 设置对齐方式  ALIGN_NORMAL==正常     ALIGN_OPPOSITE==相反     ALIGN_CENTER==居中*/
fun SpannableStringBuilder.setAlign(@Nullable align: Layout.Alignment, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(AlignmentSpan.Standard(align), 0, length, defFlags)
}

/** 设置斜体*/
fun SpannableStringBuilder.setItalic() {
    setStyle(Typeface.ITALIC)
}

/** 设置粗体*/
fun SpannableStringBuilder.setBold() {
    setStyle(Typeface.BOLD)
}

/** 设置粗体且斜体*/
fun SpannableStringBuilder.setBoldItalic() {
    setStyle(Typeface.BOLD_ITALIC)
}

/** 设置字体样式*/
fun SpannableStringBuilder.setStyle(style: Int, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(StyleSpan(style), 0, length, defFlags)
}

/** 设置字体  "monospace", "serif", and "sans-serif" */
fun SpannableStringBuilder.setTypeface(family: String, defFlags: Int = Spanned.SPAN_INCLUSIVE_INCLUSIVE) {
    setSpan(TypefaceSpan(family), 0, length, defFlags)
}

/** 设置上标，数学公式中用到。*/
fun SpannableStringBuilder.setUp(defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    val parcel = Parcel.obtain()
    parcel.writeString(this.toString())
    setSpan(SuperscriptSpan(parcel), 0, length, defFlags)
    parcel.recycle()
}

/** 设置下标，数学公式中用到。*/
fun SpannableStringBuilder.setDown(defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    val parcel = Parcel.obtain()
    parcel.writeString(this.toString())
    setSpan(SubscriptSpan(parcel), 0, length, defFlags)
    parcel.recycle()
}

/** 设置文字下划线*/
fun SpannableStringBuilder.setUnderline(defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(UnderlineSpan(), 0, length, defFlags)
}

/** 设置文字删除线*/
fun SpannableStringBuilder.setStrikethrough(defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(StrikethroughSpan(), 0, length, defFlags)
}

/** 设置文字缩放*/
fun SpannableStringBuilder.setScale(proportion: Float, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(RelativeSizeSpan(proportion), 0, length, defFlags)
}

/** 设置文字横向缩放*/
fun SpannableStringBuilder.setScaleX(proportion: Float, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(ScaleXSpan(proportion), 0, length, defFlags)
}

/** 设置列表标记,小圆点效果,gapWidth==与圆点间隔距离，color==圆点颜色*/
fun SpannableStringBuilder.setBullet(gapWidth: Int, color: Int, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(BulletSpan(gapWidth, color), 0, length, defFlags)
}

/** 设置缩进*/
fun SpannableStringBuilder.setMarginLeft(first: Int, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(LeadingMarginSpan.Standard(first, 0), 0, length, defFlags)
}

/** 设置引用线的颜色*/
fun SpannableStringBuilder.setQuoteColor(@ColorInt color: Int, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(QuoteSpan(color), 0, length, defFlags)
}

/** 设置背景色*/
fun SpannableStringBuilder.setBackgroundColor(@ColorInt color: Int, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(BackgroundColorSpan(color), 0, length, defFlags)
}

/** 设置字体颜色*/
fun SpannableStringBuilder.setColor(@ColorInt color: Int, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(ForegroundColorSpan(color), 0, length, defFlags)
}

/** 设置点击事件*/
fun SpannableStringBuilder.setClick(clickSpan: ClickableSpan, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(clickSpan, 0, length, defFlags)
}

/** 设置字体尺寸*/
fun SpannableStringBuilder.setSize(size: Number, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(AbsoluteSizeSpan(size.toFloat().sp.toInt()), 0, length, defFlags)
}

/** 设置字体模糊  Blur.NORMAL=模糊、Blur.SOLID=模糊加黑、Blur.OUTER=空心、Blur.INNER=变细*/
fun SpannableStringBuilder.setBlur(radius: Float, style: BlurMaskFilter.Blur = BlurMaskFilter.Blur.NORMAL, defFlags: Int = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) {
    setSpan(MaskFilterSpan(BlurMaskFilter(radius, style)), 0, length, defFlags)
}

/** 设置字体浮雕*/
fun SpannableStringBuilder.setEmboss(defFlags: Int = Spanned.SPAN_INCLUSIVE_INCLUSIVE) {

    setSpan(MaskFilterSpan(EmbossMaskFilter(listOf(3f, 3f, 9f).toFloatArray(), 3f, 12f, 16f)), 0, length, defFlags)
} //Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含start和end所在的端点                 (a,b)
//Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start，但包含end所在的端点       (a,b]
//Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含start，但不包含end所在的端点          [a,b)
//Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含start和end所在的端点                       [a,b]


/**
 * 不带下划线的URLSpan
 */
open class KSpan(url: String = "", val isUnderlineText: Boolean = false) : URLSpan(url) {
    override fun updateDrawState(drawState: TextPaint) {
        super.updateDrawState(drawState)
        drawState.isUnderlineText = isUnderlineText
        drawState.setColor(ResUtil.getColor("#0278FF")); //设置颜色
    }
}
